// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    bg: "../../images/werewolf.jpg",
    templateTip: "",
    situation: [2, 2, 2],
    priesthood: [{
      value: "预言家",
      name: "预言家",
      checked: true
    }, {
      value: "女巫",
      name: "女巫",
      checked: true
    }, {
      value: "猎人",
      name: "猎人",
      checked: false
    }, {
      value: "守卫",
      name: "守卫",
      checked: false
    }, {
      value: "白痴",
      name: "白痴",
      checked: false
    }],
    priesthoodNum: 2,
    identity: ["未知", "狼人", "村民"],
    status: ["存活", "公投", "狼刀", "自爆"],
    user: {
      identity: 0,
      status: "存活",
      userClass: "alive",
      statusClass: "status",
      check: false,
      rescued: false
    },
    userList: [],
    isStart: true,
    tip: `
    天黑请闭眼
    
    狼人请睁眼
    狼人请确认同伴（第一晚）
    狼人请杀人
    狼人请闭眼
    
    女巫请睁眼
    女巫，今晚Ta死了（手指指向死者）
    是否使用解药，是否使用毒药（大拇指朝上使用解药，大拇指朝下使用毒药）
    女巫请选择目标
    女巫请闭眼
    
    预言家请睁眼
    今天晚上你要验谁
    这个人是（大拇指朝上为好人，朝下为坏人。）
    预言家请闭眼
    
    猎人请睁眼
    （大拇指朝上可以开枪，大拇指朝下不能开枪）
    猎人请闭眼
    
    天亮了
    昨晚平安夜（没有人死）
    昨晚死的Ta（有人死了）
    请留遗言（第一晚的死者）
    
    平安夜可以选择任意一人开始发言
    有人死了，选择从死者左边或右边开始发言
    
    白天投出的玩家有遗言
    `,
    isTip: false
  },

  onShareAppMessage() {

  },

  toggleTip() {
    let isTip = !this.data.isTip;
    this.setData({
      isTip: isTip
    })
  },

  selectPriesthood(e) {
    let priesthood = e.detail.value
    let num = priesthood.length;
    let preisthoodList = this.data.priesthood
    preisthoodList.forEach(item => {
      item.checked = priesthood.indexOf(item.name) < 0 ? false : true;
    })
    this.setData({
      priesthood: preisthoodList,
      priesthoodNum: num
    })
  },

  startGame(e) {
    let werewolf = e.detail.value.werewolf;
    let wolfKing = e.detail.value.wolfKing;
    let villager = e.detail.value.villager;
    let priesthood = e.detail.value.priesthood;
    let situation = [werewolf, villager, priesthood.length];

    let num = werewolf + villager + priesthood.length;
    let user = this.data.user;
    let userList = new Array(num).fill(null).map(item => {
      item = Object.assign({}, user);
      return item;
    });
    let identity = ["未知", "村民", "狼人"];
    if (wolfKing) identity.push("狼王");
    identity = identity.concat(priesthood);

    let status = ["存活", "公投", "狼刀", "自爆"];
    if (priesthood.indexOf("女巫") >= 0) status.push("毒杀");
    if (priesthood.indexOf("猎人") >= 0) status.push("枪杀");
    if (priesthood.indexOf("守卫") >= 0) status.push("守卫");

    let templateTip = werewolf + "狼" + villager + "民" + priesthood.length + "神";
    this.setData({
      templateTip: templateTip,
      globalSituation: situation,
      situation: situation,
      userList: userList,
      identity: identity,
      status: status,
      isStart: false
    })
  },

  selectIdentity(e) {
    let userIdentity = "userList[" + e.currentTarget.dataset.user + "].identity";
    this.setData({
      [userIdentity]: e.detail.value
    });
    this.resetSituation();
  },

  selectStatus(e) {
    let status = this.data.status[e.detail.value]
    let user = this.data.userList[e.currentTarget.dataset.user];
    let userStatus = "userList[" + e.currentTarget.dataset.user + "]";
    
    user.status = status;
    switch (status) {
      case "狼刀":
        user.userClass = "death";
        user.statusClass = "death_status";
        break;
      case "公投":
      case "自爆":
        user.userClass = "death";
        user.statusClass = "referendum_status";
        break;
      case "枪杀":
        user.userClass = "death";
        user.statusClass = "shot_status";
        break;
      case "毒杀":
        user.userClass = "death";
        user.statusClass = "poison_status";
        break;
      case "守卫":
        user.userClass = "alive";
        user.statusClass = "guard_status";
        break;
      default:
        user.userClass = "alive";
        user.statusClass = "status";
        break;
    }
    this.setData({
      [userStatus]: user
    })

    this.resetSituation();
  },

  resetSituation() {
    let situation = [0, 0, 0];
    let userList = this.data.userList;
    let identity = this.data.identity;
    let death_status = ["狼刀", "公投", "自爆", "毒杀", "枪杀"];

    userList.forEach(item => {
      if (death_status.indexOf(item.status) >= 0) {
        switch (identity[item.identity]) {
          case "村民":
            situation[1] -= 1;
            break;
          case "狼人":
          case "狼王":
            situation[0] -= 1;
            break;
          case "未知":
            break;
          default:
            situation[2] -= 1;
        }
      }
    })
    situation = this.data.globalSituation.map((item, index) => {
      let value = item + situation[index];
      value = value >= 0 ? value : 0;
      return value;
    })
    this.setData({
      situation: situation
    })
  },

  changeCheck(e) {
    let userCheck = "userList[" + e.currentTarget.dataset.user + "].check";
    this.setData({
      [userCheck]: e.detail.value
    });
  },

  changeRescued(e) {
    let userRescued = "userList[" + e.currentTarget.dataset.user + "].rescued";
    this.setData({
      [userRescued]: e.detail.value
    });
  },

  resetGame() {
    let length = this.data.userList.length;
    let user = this.data.user;
    let userList = new Array(length).fill(null).map(item => {
      item = Object.assign({}, user);
      return item;
    });
    this.setData({
      userList: userList
    })
  },

  back() {
    this.setData({
      isStart: true
    })
  }
})